package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.SoulQuestion;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Desciption:test
 * @Create by:金木yan
 * @Date: 2021/12/11 15:30
 */
@DubboService
public class SoulQuestionsApiImpl implements SoulQuestionsApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    //根据问卷id查询试题集合
    @Override
    public Map<ObjectId, List<SoulQuestion>> findByQuestionnaireId(ObjectId questionnaireId) {
        //1.查询试题集合
        Query query = Query.query(Criteria.where("questionnaireId").is(questionnaireId));
        List<SoulQuestion> questions = mongoTemplate.find(query, SoulQuestion.class);
        //2.构建map
        Map<ObjectId, List<SoulQuestion>> map = new HashMap<>();
        map.put(questionnaireId, questions);
        return map;
    }

    //根据试题id，查询问卷id
    @Override
    public ObjectId findById(String questionId) {
        SoulQuestion question = mongoTemplate.findById(new ObjectId(questionId), SoulQuestion.class);
        return question.getQuestionnaireId();
    }
}
